<template>
	<view class="uploads">
		<view class="" v-if="saveAdress">
			<view class="w94 radius20 bgwhite mt20" style="padding: 24rpx;">
				<view class="" style="padding: 24rpx 0;">
					设置居住地址
				</view>
				<text style="font-size: 24rpx;color: #9D9D9D;">平台会根据您设置的居住地址为中心点，由近到远推送订单。</text>
				<view class="w94 radius20 flex"
					style="justify-content: space-between;margin: 40rpx 0;margin-bottom: 0; padding: 15rpx;">
					<view class="">
						<text style="color: red;">*</text>居住地址：
					</view>
					<view class="" @click="dw" style="width: 60%;">
						<u-icon label="定位到具体地址" v-if="!locations" labelPos="left" name="arrow-right"></u-icon>
						<block v-else>{{locations}}</block>
					</view>
				</view>
			</view>
			<block v-if="buttonType==1">
				<view class=""
					style="box-shadow: 1px -5px 10px #ccc;position: absolute;bottom: 0;padding: 30rpx; background-color: #fff;display: flex;justify-content: space-around;width: 100%;">
					<view class="" style="padding: 24rpx;color: #d9d9d9;" @click="submit">
						跳过
					</view>
					<view class=""
						style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;"
						@click="submit">
						下一步
					</view>
				</view>
			</block>
			<block v-else-if="buttonType==2">
				<view class=""
					style="box-shadow: 1px -5px 10px #ccc;position: absolute;bottom: 0;padding: 30rpx; background-color: #fff;display: flex;justify-content: space-around;width: 100%;">
					<view class=""
						style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;"
						@click="saveSet">
						保存设置
					</view>
				</view>
			</block>
		</view>
		<view class="u-wrap" v-else>
			<!-- <view style="background-color: #fff;padding: 15rpx;">
				<search placeText="请输入您擅长的技术" bg="#F4F4F4" @input="onInput"></search>
			</view> -->
			<view class="u-menu-wrap">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in tabbar.data" :key="index" class="u-tab-item"
						:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
						@tap.stop="swichMenu(index)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<block v-for="(item,index) in tabbar.data" :key="index">
					<scroll-view scroll-y class="right-box" v-if="current==index">
						<view class="page-view" v-for="(item1, index1) in item.foods" :key="index1">
							<view>
								{{item1.name}}
							</view>
							<view class="">
								<label>
									<checkbox :value="item1.id" @click="pushId(item1.id)"
										:checked="isCheck(item1.id)" />
								</label>
							</view>
						</view>
					</scroll-view>
				</block>
			</view>
			<block v-if="buttonType==1">
				<view class=""
					style="box-shadow: 1px -5px 10px #ccc;position: absolute;bottom: 0;padding: 30rpx; background-color: #fff;display: flex;justify-content: space-around;width: 100%;">
					<view class="" style="padding: 24rpx;color: #d9d9d9;" @click="submit">
						跳过
					</view>
					<view class=""
						style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;"
						@click="submit">
						下一步
					</view>
				</view>
			</block>
			<block v-else-if="buttonType==3">
				<view class=""
					style="box-shadow: 1px -5px 10px #ccc;position: absolute;bottom: 0;padding: 30rpx; background-color: #fff;display: flex;justify-content: space-around;width: 100%;">
					<view class=""
						style="width: 80%;height: 90rpx;border-radius: 90rpx;background-color: #CEEE5B;text-align: center;line-height: 2.5;"
						@click="save">
						保存
					</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import {
		profile,
		skill,
	} from '@/common/teacherReq';
	import classifyData from "@/common/classify.data.js";
	import search from '@/components/search.vue';
	export default {
		data() {
			return {
				tabbar: classifyData,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				saveAdress: true,
				buttonType: 1,
				locations: '',
				IdList: [], // 技能id数组
			}
		},
		components: {
			search
		},
		computed: {
			info() {
				return uni.getStorageSync('info')
			}
		},
		methods: {
			pushId(id) {
				if (this.IdList.includes(id)) {
					this.IdList = this.IdList.filter(item => item !== id)
					return
				} else if (this.IdList.length == 3) {
					uni.showToast({
						title: '只能选择三个',
						icon: 'error'
					})
					return
				}
				this.IdList.push(id)
				console.log(this.IdList, 'idlist222')
			},
			isCheck(id) {
				if (!id || this.IdList.length == 0) return false
				if (this.IdList.includes(id)) {
					return true
				} else {
					return false
				}
			},
			async submit() {
				// uni.setStorageSync('login', true)
				// uni.switchTab({
				// 	url: '/pages/index/index'
				// })
				if (this.buttonType == 1 && this.saveAdress) {
					this.saveAdress = false
					let res = await profile({
						username: "",
						nickname: "",
						avatar: "",
						rest_off: 1,
						contact: "",
						work_photo: "",
						address: this.locations
					})
					this.tabbar = await skill({})
					console.log('skill:--', this.tabbar)
					uni.setNavigationBarTitle({
						title: '服务技术'
					})
					return
				}
				if (this.IdList.length) {
					await profile({
						username: "",
						nickname: "",
						avatar: "",
						rest_off: 1,
						contact: "",
						work_photo: "",
						address: this.locations,
						skill: this.IdList
					})
				}
				if (!this.saveAdress) {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
			saveAdressFun() {
				this.saveAdress = false
				profile({
					username: "",
					nickname: "",
					avatar: "",
					rest_off: 1,
					contact: "",
					work_photo: "",
					address: this.locations
				}).then(res => {
					console.log(res, 'res')
					// this.toPage('/subpack/login/adress?type=1')
					uni.setNavigationBarTitle({
						title: '服务技术'
					})
				})
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			onInput(e) {
				console.log('e', e)
			},
			saveSet() {
				console.log("saveSet");
			},
			save() {
				console.log("save");
			},
			dw() {
				uni.chooseAddress({
					success: (res) => {
						console.log(res.userName)
						console.log(res.postalCode)
						console.log(res.provinceName)
						console.log(res.cityName)
						console.log(res.countyName)
						console.log(res.detailInfo)
						console.log(res.nationalCode)
						console.log(res.telNumber)
						this.locations = res.provinceName + res.cityName + res.countyName + res.detailInfo
					}
				})
			}
		},
		onLoad(opt) {
			this.buttonType = Number(opt.type)
			if (this.buttonType == 3) {
				this.saveAdressFun()
			}
			console.log('opt: ', this.buttonType, opt);
		}
	}
</script>

<style lang="scss" scoped>
	.uploads {
		position: relative;
		height: calc(100vh - 20rpx);

		.u-wrap {
			display: flex;
			flex-direction: column;
			height: 100vh;
			position: relative;

			.u-menu-wrap {
				flex: 1;
				display: flex;
				overflow: hidden;

				.u-tab-view {
					height: 100%;
				}

				.u-tab-view {
					width: 200rpx;
					height: 100%;

					.u-tab-item {
						height: 110rpx;
						background: #f6f6f6;
						box-sizing: border-box;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 26rpx;
						color: #444;
						font-weight: 400;
						line-height: 1;
					}

					.u-tab-item-active {
						position: relative;
						color: #000;
						font-size: 30rpx;
						font-weight: 600;
						background: #fff;
					}

					.u-tab-item-active::before {
						content: "";
						position: absolute;
						border-left: 4px solid $uni-color-primary;
						height: 32rpx;
						left: 0;
						top: 39rpx;
					}
				}
			}
		}




		.right-box {
			background-color: rgb(255, 255, 255);

			.page-view {
				padding: 16rpx;
				border-bottom: 2rpx solid #c9c9c9;
				height: 90rpx;
				line-height: 2;
				display: flex;
				justify-content: space-between;

				.class-item {
					margin-bottom: 30rpx;
					background-color: #fff;
					padding: 16rpx;
					border-radius: 8rpx;

					.item-title {
						font-size: 26rpx;
						color: #000;
						font-weight: bold;
					}
				}

				.item-container {
					display: flex;
					flex-wrap: wrap;

					.thumb-box {
						width: 33.333333%;
						display: flex;
						align-items: center;
						justify-content: center;
						flex-direction: column;
						margin-top: 20rpx;

						.item-menu-name {
							font-weight: normal;
							font-size: 24rpx;
							color: #000;
						}

						.item-menu-image {
							width: 120rpx;
							height: 120rpx;
						}
					}

				}
			}
		}







	}
</style>